package peppa.george.movie.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import peppa.george.base.Util.PageUtil;
import peppa.george.movie.domain.Movie;
import peppa.george.movie.mapper.MovieMapper;
import peppa.george.movie.query.MovieQuery;
import peppa.george.movie.service.IMovieService;

import java.util.ArrayList;
import java.util.List;

@Service
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class MovieServiceImpl implements IMovieService {
    @Autowired
    private MovieMapper movieMapper;
    public List<Movie> findAll() {
        return movieMapper.findAll();
    }

    public Movie findById(Long id) {
        return movieMapper.findById(id);
    }

    public void save(Movie movie) {
        movieMapper.save(movie);
    }

    public void delete(Long id) {
        movieMapper.delete(id);
    }

    public void update(Movie movie) {
        movieMapper.update(movie);
    }

    public PageUtil<Movie> queryByPage(MovieQuery movieQuery) {
        Integer totals = movieMapper.queryByCount(movieQuery);
        if (totals == null || totals == 0){
            return new PageUtil<Movie>(0,new ArrayList<Movie>());
        }
        List<Movie> movies = movieMapper.queryByPage(movieQuery);
        return new PageUtil<>(totals,movies);
    }
}
